//
//  UIView+HKSafeArea.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2021/7/14.
//

#import "UIView+HKSafeArea.h"

@implementation UIView (HKSafeArea)

- (UIEdgeInsets)ks_safeAreaInsets {
#if defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
    if (@available(iOS 11.0, *)) {
        return self.safeAreaInsets;
    } else {
        return UIEdgeInsetsZero;
    }
#else
    return UIEdgeInsetsZero;
#endif
}

- (UIViewController *)ks_getCurrentViewController {
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

- (MASViewAttribute *)ks_safeAreaLeft {
    if (@available(iOS 11.0, *)) {
        return self.mas_safeAreaLayoutGuideLeft;
    }
    return self.mas_left;
}

- (MASViewAttribute *)ks_safeAreaTop {
    if (@available(iOS 11.0, *)) {
        return self.mas_safeAreaLayoutGuideTop;
    }
    return [self ks_getCurrentViewController].mas_topLayoutGuide ?: self.mas_top;
}

- (MASViewAttribute *)ks_safeAreaRight {
    if (@available(iOS 11.0, *)) {
        return self.mas_safeAreaLayoutGuideRight;
    }
    return self.mas_right;
}

- (MASViewAttribute *)ks_safeAreaBottom {
    if (@available(iOS 11.0, *)) {
        return self.mas_safeAreaLayoutGuideBottom;
    }
    return [self ks_getCurrentViewController].mas_bottomLayoutGuideBottom ?: self.mas_bottom;
}

- (CGFloat)ks_portraitTopInset {
    UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
    if (@available(iOS 13.0, *)) {
        orientation = [UIApplication sharedApplication].keyWindow.windowScene.interfaceOrientation;
    } else {
        orientation = [UIApplication sharedApplication].statusBarOrientation;
    }
    UIEdgeInsets insets = [self ks_safeAreaInsets];
    switch (orientation) {
        case UIInterfaceOrientationUnknown:
            return 0.f;
        case UIInterfaceOrientationPortrait:
            return insets.top;
        case UIInterfaceOrientationPortraitUpsideDown:
            return insets.bottom;
        case UIInterfaceOrientationLandscapeLeft:
            return insets.right;
        case UIInterfaceOrientationLandscapeRight:
            return insets.left;
    }
}

- (CGFloat)ks_portraitBottomInset {
    UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
    if (@available(iOS 13.0, *)) {
        orientation = [UIApplication sharedApplication].keyWindow.windowScene.interfaceOrientation;
    } else {
        orientation = [UIApplication sharedApplication].statusBarOrientation;
    }
    UIEdgeInsets insets = [self ks_safeAreaInsets];
    switch (orientation) {
        case UIInterfaceOrientationUnknown:
            return 0.f;
        case UIInterfaceOrientationPortrait:
            return insets.bottom;
        case UIInterfaceOrientationPortraitUpsideDown:
            return insets.top;
        case UIInterfaceOrientationLandscapeLeft:
            return insets.left;
        case UIInterfaceOrientationLandscapeRight:
            return insets.right;
    }
}

@end
